package handler.interceptor;

import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;

import bean.User;

public class OfflineStateHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler)
            throws Exception {
        HttpSession session = req.getSession();
        Object userName = session.getAttribute("user-name");
        if (User.verifyUserName((String) userName)) {
            String referer = req.getHeader("referer");
            if (Objects.nonNull(referer) && referer.length() > 0) {
                resp.sendRedirect(referer);
            } else {
                resp.sendRedirect("home");
            }
            return false;
        }
        return true;
    }

}
